package com.lams.action.system;

import java.util.*;
import net.sf.json.JSONArray;



import com.lams.action.SubAction;
import com.lams.service.system.IModuleFacade;
import com.lams.util.ResultEntity;
import com.lams.util.TreeEntity;
import com.opensymphony.xwork2.ActionContext;

public class ModuleAction extends SubAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3360258888802311188L;
	// 针对于页面的成员变量
	private IModuleFacade mf;
	private String ROLE_ID;
	// 构造方法，用于对成员变量赋初值
	public ModuleAction() {
	}

	public IModuleFacade getMf() {
		return mf;
	}

	public void setMf(IModuleFacade mf) {
		this.mf = mf;
	}
	private String node;
	
	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public String getROLE_ID() {
		return ROLE_ID==null?"-1":ROLE_ID;
	}

	public void setROLE_ID(String rOLE_ID) {
		ROLE_ID = "".equals(rOLE_ID)|| rOLE_ID==null?"-1":rOLE_ID;
	}

	// 处理findModule请求的方法
	@SuppressWarnings("unchecked")
	public String findModule() {
		Map session = ActionContext.getContext().getSession();
		ResultEntity result = new ResultEntity();
		if(session.get("role")==null){
			result.setSuccess(false);
			result.setMessage("您尚未未登陆本系统，请登陆！");
			return INPUT;
		}
		List<TreeEntity> trees = mf.findModuleByMR((List<Integer>) session.get("role"), Integer.parseInt(node));
		
		System.out.println(JSONArray.fromObject(trees).toString());
		this.setResult(JSONArray.fromObject(trees).toString());
		return SUCCESS;
	}
	public String getMenu(){
		Map session = ActionContext.getContext().getSession();
		ResultEntity result = new ResultEntity();
		if(session.get("role") == null){
			result.setSuccess(false);
			result.setMessage("您尚未未登陆本系统，请登陆！");
		}else{
			List<TreeEntity> tree = mf.getMenu((List<Integer>) session.get("role"));
			result.setData(tree);
			result.setSuccess(true);
		}
		this.setResult(ResultEntity.decode(result));
		return SUCCESS;
	}
	public String showAllModuleAndFun(){
		List data = mf.getAllMF(this.getROLE_ID());
		ResultEntity result = new ResultEntity();
		result.setData(data);
		result.setTotalCount(data.size());
		result.setSuccess(true);
		this.setResult(ResultEntity.decode(result));
		return SUCCESS;
	}
}
